using System;
using System.Collections;

namespace ELS.Configuration
{
	#region ConfigurationPropertyCollection
	/// <summary>
	/// Represents a collection of string values.
	/// </summary>
	public class ConfigurationPropertyCollection: IEnumerable
	{
		#region variables
		private HashList _innerList = new HashList ();
		private ConfigurationObjectBase _parent;
		#endregion variables

		#region constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/> class.
		/// </summary>
		/// <param name="parent">an instance of <see cref="ELS.Configuration.ConfigurationObjectBase"/>.</param>
		internal ConfigurationPropertyCollection( ConfigurationObjectBase parent )
		{
			_parent = parent;
		}
		#endregion constructors

		#region IEnumerable Members
		/// <summary>
		/// Returns an enumerator that can iterate through the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>
		/// </summary>
		/// <returns></returns>
		public IEnumerator GetEnumerator()
		{
			return _innerList.Values.GetEnumerator();
		}
		#endregion

		#region public methods
		/// <summary>
		/// Adds entries to the current <see cref="ELS.Configuration.ConfigurationPropertyCollection"/> 
		/// </summary>
		/// <param name="property">The <see cref="ELS.Configuration.ConfigurationProperty"/> to be added to the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/></param>
		/// <returns> the <see cref="ELS.Configuration.ConfigurationProperty"/> that has been added to the collection. </returns>
		public ConfigurationProperty Add (ConfigurationProperty property)
		{
			property.Parent = _parent;
			_innerList.Add ( property.Name, property );
			return property;
		}

		/// <summary>
		/// Adds entries to the current <see cref="ELS.Configuration.ConfigurationPropertyCollection"/> 
		/// </summary>
		/// <param name="name">The name of the <see cref="ELS.Configuration.ConfigurationProperty"/> to be added to the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/></param>
		/// <param name="value">The value of the <see cref="ELS.Configuration.ConfigurationProperty"/> to be added to the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/></param>
		/// <returns> the <see cref="ELS.Configuration.ConfigurationProperty"/> that has been added to the collection. </returns>
		public ConfigurationProperty Add (string name, string value)
		{
			return Add( new ConfigurationProperty( name, value ) );
		}

		/// <summary>
		/// Adds entries to the current <see cref="ELS.Configuration.ConfigurationPropertyCollection"/> 
		/// </summary>
		/// <param name="name">The name of the <see cref="ELS.Configuration.ConfigurationProperty"/> to be added to the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/></param>
		/// <returns> the <see cref="ELS.Configuration.ConfigurationProperty"/> that has been added to the collection. </returns>
		public ConfigurationProperty Add (string name)
		{
			return Add( name, null );
		}

		/// <summary>
		/// Removes all elements from the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/> 
		/// </summary>
		public void Clear ()
		{
			_innerList.Clear();
		}

		/// <summary>
		/// Removes the element at the specified index.
		/// </summary>
		/// <param name="index"> The zero-based index of the element to remove. </param>
		public void RemoveAt (int index )
		{
			_innerList.RemoveAt (index );
		}

		///<summary>
		/// Removes the element from the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>.
		/// </summary>
		/// <param name="property">the <see cref="ELS.Configuration.ConfigurationProperty"/> to remove.</param>
		public void Remove ( ConfigurationProperty property )
		{
			Remove( property.Name );
		}

		///<summary>
		/// Removes the element with the specified name from the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>.
		/// </summary>
		/// <param name="name">The name of the <see cref="ELS.Configuration.ConfigurationProperty"/> to remove.</param>
		public void Remove (string name )
		{
			_innerList.Remove( name );
		}

		/// <summary>
		/// Gets the number of elements actually contained in the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>
		/// </summary>
		public int Count 
		{
			get	{ return _innerList.Count; 	}
		}

		/// <summary>
		/// Gets or sets the <see cref="ELS.Configuration.ConfigurationProperty"/> at the specified index.
		/// </summary>
		public ConfigurationProperty this [int index]
		{
			get { return (ConfigurationProperty) _innerList.GetByIndex( index ); }
			set { _innerList[ index ] = value; }
		}

		/// <summary>
		/// Gets or sets the <see cref="ELS.Configuration.ConfigurationProperty"/> with the specified name.
		/// </summary>
		public ConfigurationProperty this [string name]
		{
			get { return _innerList [ name ] as ConfigurationProperty; }
			set { _innerList [ name ] = value; }
		}

		/// <summary>
		/// Determines whether a <see cref="ELS.Configuration.ConfigurationProperty"/> with the specified name is in the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>.
		/// </summary>
		/// <param name="name">
		/// The <see cref="ELS.Configuration.ConfigurationProperty"/> with the specified name to 
		/// locate in the <see cref="ELS.Configuration.ConfigurationPropertyCollection"/>. 
		/// The value can be a null reference (Nothing in Visual Basic).
		/// </param>
		/// <returns>true if <see cref="ELS.Configuration.ConfigurationProperty"/> is found; otherwise, false.</returns>
		public bool Contains ( string name )
		{
			return _innerList.ContainsKey ( name );
		}

		#endregion public methods
	}
	#endregion ConfigurationPropertyCollection
}
